package com.buer.train.api;

import com.buer.train.business.domain.DeptDO;
import com.buer.train.business.domain.TeacherDO;
import com.buer.train.business.service.DeptService;
import com.buer.train.business.service.TeacherService;
import com.buer.train.common.annotation.Auth;
import com.buer.train.common.eumn.ResultEnum;
import com.buer.train.common.holder.LoginUser;
import com.buer.train.common.holder.LoginUserHolder;
import com.buer.train.common.utils.BaseResult;
import com.buer.train.common.utils.SexUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/teacher")
@Api(tags = {"教师相关接口"})
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private DeptService deptService;

    @RequestMapping(value = "/teacher/list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("教师列表")
    @Auth(value = "teacher:teacher:list")
    public BaseResult teacherList(@RequestBody Map<String, Object> params) {
        LoginUser loginUserHolder = LoginUserHolder.getLoginUserHolder();
        params.put("depts",loginUserHolder.getNodeDepts());

        PageInfo<TeacherDO> teacherList = teacherService.getTeacherList(params);
        Map map = new HashMap<>();
        map.put("list", teacherList.getList());
        map.put("total", teacherList.getTotal());
        return BaseResult.build(ResultEnum.SUCCESS, map);
    }

    @RequestMapping(value = "/teacher/getAllTeacher", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("获取所有教师")
    @Auth
    public BaseResult getAllTeacher(@RequestBody Map<String, Object> params) {
        LoginUser loginUserHolder = LoginUserHolder.getLoginUserHolder();
        params.put("depts",loginUserHolder.getNodeDepts());
        List<TeacherDO> teacherDOList = teacherService.list(params);
        return BaseResult.build(ResultEnum.SUCCESS,teacherDOList);
    }

    @RequestMapping(value = "/teacher/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("新增教师")
    @Auth(value = "teacher:teacher:add")
    public BaseResult teacherAdd(@RequestBody TeacherDO teacher) {
        LoginUser loginUserHolder = LoginUserHolder.getLoginUserHolder();
        String s = SexUtil.userType(teacher.getCardNo());
        teacher.setDeptId(loginUserHolder.getDeptId());
        teacher.setSex(s);
        teacher.setCreateTime(new Date());
        teacher.setUpdateTime(new Date());
        if(teacherService.save(teacher)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/teacher/remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID删除教师")
    @Auth(value = "teacher:teacher:remove")
    public BaseResult teacherRemove(@RequestBody Map<String, Object> params) {
        if (Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.FAILURE);
        }
        Integer id = Integer.valueOf(params.get("id") + "");

        if(teacherService.remove(id)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/teacher/update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("修改教师")
    @Auth(value = "teacher:teacher:update")
    public BaseResult teacherUpdate(@RequestBody TeacherDO teacher) {
        LoginUser loginUserHolder = LoginUserHolder.getLoginUserHolder();
        TeacherDO teacherDO = teacherService.get(teacher.getId());
        teacherDO.setJobDuties(teacher.getJobDuties());
        teacherDO.setJobTitle(teacher.getJobTitle());
        teacherDO.setCity(teacher.getCity());
        teacherDO.setArea(teacher.getArea());
        teacherDO.setCardNo(teacher.getCardNo());
        teacherDO.setEmail(teacher.getEmail());
        teacherDO.setGraduatedSchool(teacher.getGraduatedSchool());
        teacherDO.setHeadImg(teacher.getHeadImg());
        teacherDO.setNation(teacher.getNation());
        teacherDO.setDeptId(loginUserHolder.getDeptId());
        teacherDO.setHighestEducation(teacher.getHighestEducation());
        teacherDO.setPhone(teacher.getPhone());
        teacherDO.setProfession(teacher.getProfession());
        teacherDO.setProvince(teacher.getProvince());
        teacherDO.setRemark(teacher.getRemark());
        teacherDO.setTeachingSection(teacher.getTeachingSection());
        teacherDO.setTeachingSubject(teacher.getTeachingSubject());
        teacherDO.setUpdateTime(new Date());
        if(teacherService.update(teacher)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);

    }

    @RequestMapping(value = "/teacher/get", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID查询教师")
    @Auth
    public BaseResult teacherGet(@RequestBody Map<String, Object> params) {
        if(Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.PARAME_ERROR);
        }
        Integer id = Integer.valueOf(params.get("id") + "");
        TeacherDO teacherDO = teacherService.get(id);

        return BaseResult.build(ResultEnum.SUCCESS,teacherDO);
    }

    @RequestMapping(value = "/teacher/getAllTeacherByDeptId", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据deptId获取所有教师")
    @Auth
    public BaseResult getAllTeacherByDeptId(@RequestBody Map<String, Object> params) {
        if(Objects.isNull(params.get("deptId"))){
            return BaseResult.build(ResultEnum.PARAME_ERROR);
        }

        Integer deptId = Integer.valueOf(params.get("deptId") + "");

        Set<Integer> nodeDeptChilds=new HashSet<>();
        nodeDeptChilds.add(deptId);
        getCurrNodeDeptChilds(deptId,nodeDeptChilds);
        params.put("depts",nodeDeptChilds);
        List<TeacherDO> teacherDOList = teacherService.list(params);
        return BaseResult.build(ResultEnum.SUCCESS,teacherDOList);
    }

    private void getCurrNodeDeptChilds(Integer deptId,Set<Integer> nodeDeptChilds) {
        Map<String,Object> map=new HashMap<>();
        map.put("parentId",deptId);
        map.put("type","0");
        List<DeptDO> list = deptService.list(map);
        for (DeptDO deptDO:list){
            nodeDeptChilds.add(deptDO.getDeptId());
            getCurrNodeDeptChilds(deptDO.getDeptId(),nodeDeptChilds);
        }
    }
}
